<?php
/**
 * author      : 道古今
 * createTime  : 2020/7/19  22:15
 * description :
 */

namespace App\Aliapi;


use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class SendTemplateSMS
{
    /**
     * @param $accessKeyId
     * @param $accessSecret
     * @param $SignName      签名
     * @param $TemplateCode  模板id
     * @param $phone  手机号
     * @param $TemplateParam  变量json
     * @return array
     * @throws ClientException
     */
    public function sendTemplateSMS($accessKeyId,$accessSecret,$SignName,$TemplateCode,$phone,$TemplateParam)
    {
        AlibabaCloud::accessKeyClient($accessKeyId, $accessSecret)
            ->regionId('cn-hangzhou')
            ->asDefaultClient();
        try {
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host('dysmsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => $phone,
                        'SignName' => $SignName,
                        'TemplateCode' => $TemplateCode,
                        'TemplateParam' => $TemplateParam,
                    ],
                ])
                ->request();
            return $result->toArray();
//            print_r($result->toArray());
        }catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }

    }

}
